[−][src]Crate prometheus
The Rust client library for Prometheus.
Use of this library involves a few core concepts:
-
Metric
s likeCounter
s that represent information about your system. -
An endpoint that calls
gather
which returnsMetricFamily
s through anEncoder
.
Basic Example
use prometheus::{Opts, Registry, Counter, TextEncoder, Encoder}; // Create a Counter. let counter_opts = Opts::new("test_counter", "test counter help"); let counter = Counter::with_opts(counter_opts).unwrap(); // Create a Registry and register Counter. let r = Registry::new(); r.register(Box::new(counter.clone())).unwrap(); // Inc. counter.inc(); // Gather the metrics. let mut buffer = vec![]; let encoder = TextEncoder::new(); let metric_families = r.gather(); encoder.encode(&metric_families, &mut buffer).unwrap(); // Output to the standard output. println!("{}", String::from_utf8(buffer).unwrap());
You can find more examples within
/examples
.
Static Metrics
This crate supports staticly built metrics. You can use it with
lazy_static
to quickly build up and collect
some metrics.
#[macro_use] extern crate lazy_static; #[macro_use] extern crate prometheus; use prometheus::{self, IntCounter, TextEncoder, Encoder}; lazy_static! { static ref HIGH_FIVE_COUNTER: IntCounter = register_int_counter!("highfives", "Number of high fives received").unwrap(); } HIGH_FIVE_COUNTER.inc(); assert_eq!(HIGH_FIVE_COUNTER.get(), 1);
By default, this registers with a default registry. To make a report, you can call
gather
. This will return a family of metrics you can then feed through an
Encoder
and report to Promethus.
#[macro_use] extern crate prometheus; use prometheus::{self, TextEncoder, Encoder}; // Register & measure some metrics. let mut buffer = Vec::new(); let encoder = TextEncoder::new(); // Gather the metrics. let metric_families = prometheus::gather(); // Encode them to send. encoder.encode(&metric_families, &mut buffer).unwrap(); let output = String::from_utf8(buffer.clone()).unwrap(); const EXPECTED_OUTPUT: &'static str = "# HELP highfives Number of high fives received\n# TYPE highfives counter\nhighfives 1\n"; assert!(output.starts_with(EXPECTED_OUTPUT));
See prometheus_static_metric for additional functionality.
Features
This library supports four features:
gen
: To generate protobuf client with the latest protobuf version instead of using the pre-generated client.nightly
: Enable nightly only features.process
: For collecting process info.push
: Enable push support.
Modules
core | Core traits and types. |
local | Unsync local metrics, provides better performance. |
proto | Protocol buffers format of metrics. |
Macros
histogram_opts | Create a [ |
labels | Create labels with specified name-value pairs. |
opts | Create an [ |
register_counter | Create a [ |
register_counter_vec | Create a [ |
register_gauge | Create a [ |
register_gauge_vec | Create a [ |
register_histogram | Create a [ |
register_histogram_vec | Create a [ |
register_int_counter | Create an [ |
register_int_counter_vec | Create an [ |
register_int_gauge | Create an [ |
register_int_gauge_vec | Create an [ |
Structs
Histogram | A |
HistogramOpts | A struct that bundles the options for creating a |
HistogramTimer | Timer to measure and record the duration of an event. |
Opts | A struct that bundles the options for creating most |
ProtobufEncoder | An implementation of an |
Registry | A struct for registering Prometheus collectors, collecting their metrics, and gathering
them into |
TextEncoder | An implementation of an |
Enums
Error | The error types for prometheus. |
Constants
DEFAULT_BUCKETS | The default |
PROTOBUF_FORMAT | The protocol buffer format of metric family. |
TEXT_FORMAT | The text format of metric family. |
Traits
Encoder | An interface for encoding metric families into an underlying wire protocol. |
Functions
default_registry | Default registry (global static). |
exponential_buckets | Create |
gather | Return all |
linear_buckets | Create |
register | Registers a new |
unregister | Unregisters the |
Type Definitions
Counter | A |
CounterVec | A |
Gauge | A |
GaugeVec | A |
HistogramVec | A |
IntCounter | The integer version of |
IntCounterVec | The integer version of |
IntGauge | The integer version of |
IntGaugeVec | The integer version of |
Result | A specialized Result type for prometheus. |